<template>
  <!--ASN/收货单-->
  <div id="Asndetails">
    <v-details
      :detailData="inputData"
      :inputConfig="inputConfig"
      :rules="rules"
      @pop="setData"
      ref="detaRef"
    >
    </v-details>
  </div>
</template>

<script>
import details from "commons/Detail";
import baseMixin from "@/mixins/BaseMixin";

export default {
  mixins: [baseMixin],
  props: {
    inputData: {
      type: Object,
    },
  },
  name: "asnDetails",
  data() {
    //自定义验证规则,验证LPN是否合法
    const receiptkeyValidate = (rule, value, callback) => {
      if (!this.newFlag) {
        if (value != "") {
          this.$api.get(`asn/detailed/${value}`).then((res) => {
            
              if (!res.bean) {
                this.$message.error(this.$t('msg.msg55'));
                this.inputData[rule.field] = "";
                callback(new Error());
              } else {
                this.inputData.storerkey = res.bean.storerkey;
                this.$parent.flag = true;
                this.$parent.getTableData(1, {});
                callback();
              }
          });
        } else {
          this.$message.error(this.$tj('in.asnCode','app.emptyMes'));
          callback(new Error());
        }
      } else {
        callback();
      }
    };

    return {
      newFlag: false,
      rules: {
        adjustmentkey: [{ required: true, trigger: "blur" }],
        receiptkey: [
          { required: true, trigger: "blur", validator: receiptkeyValidate },
        ],
        storerkey: [{ required: true, trigger: "blur" }],
      },
    };
  },
  computed: {
    inputConfig() {
      return [
        [
          {
            type: "input",
            label: this.$t('in.adjustmentkey1'),
            disabled: true,
            props: "adjustmentkey",
          },
          {
            type: "table",
            label: this.$t('in.asnCode'),
            disabled: this.newFlag,
            props: "receiptkey",
            tableConfig: [
              {
                label: this.$t("in.receiptkey"),
                prop: "receiptkey",
                input: "input",
              },
              {
                label: this.$t("config.shipper"),
                prop: "storerkey",
                input: "input",
              },
              {
                label: this.$t('in.externreceiptkey2'),
                prop: "externreceiptkey",
                input: "input",
              },
              {
                label: this.$t("in.pokey1"),
                prop: "pokey",
                input: "input",
              },
              {
                label: this.$t("in.externpokey"),
                prop: "referencedocument",
                input: "input",
              },
              {
                label: this.$t("common.status"),
                prop: "status",
                input: "select",
                options: this.getOptions("codelkup/RECSTATUS"),
              },
              {
                label: this.$t("common.type"),
                prop: "type",
                input: "select",
                options: this.getOptions("codelkup/RECEIPTYPE"),
              },
              {
                label: this.$t("in.whether"),
                prop: "whether",
                input: "input",
              },
              {
                label: this.$t("config.carrierCode"),
                prop: "carriername",
                input: "input",
              },
              {
                label: this.$t("in.carrierreference1"),
                prop: "carrierreference",
                input: "input",
              },
              {
                label: this.$t('in.suppliersourcecode'),
                prop: "suppliersourcecode",
                input: "input",
              },
              {
                label: this.$t("config.supplier"),
                prop: "suppliername",
                input: "input",
              },
              {
                label: this.$t('in.shipfromphone'),
                prop: "shipfromphone",
                input: "input",
              },
              {
                label: this.$t('app.editName'),
                prop: "editwho",
                input: "input",
              },
              {
                label: this.$t("in.rtxdelorderno"),
                prop: "rtxdelorderno",
                input: "input",
              },
              {
                label: this.$t("in.rtxpofromhourse1"),
                prop: "rtxpofromhourse",
                input: "input",
              },
              {
                label: this.$t("in.fdescription"),
                prop: "fdescription",
                input: "input",
              },
              {
                label: this.$t("in.qtyexpected"),
                prop: "totalqtyexpected",
                input: "input",
              },
              {
                label: this.$t("in.qtyreceived"),
                prop: "totalqtyreceived",
                input: "input",
              },
              {
                label: this.$t("in.lastreceiptdate"),
                prop: "receiptdate",
                input: "date",
              },
              {
                label: this.$t("in.closeddate1"),
                prop: "closeddate",
                input: "date",
              },
              {
                label: this.$t("in.receiptfinishflag"),
                prop: "receiptfinishflag",
                input: "input",
              },
              {
                label: this.$t('in.returnstate'),
                prop: "msg",
                input: "input",
              },
            ],
            url: "/queryListOfDark2/",
          },
          {
            type: "input",
            label: this.$t("config.shipper"),
            disabled: true,
            props: "storerkey",
          },
          {
            type: "date",
            label: this.$t("app.effectivedate"),
            disabled: true,
            props: "effectivedate",
          },
        ],
      ];
    },
  },
  methods: {
    setData(data, id, close, listId) {
      this.inputData.receiptkey = data.receiptkey;
      this.inputData.storerkey = data.storerkey;
      this.$parent.flag = true;
      if (this.$parent.temporary.length > 0) {
        this.$parent.$refs.table.delSelectData(this.$parent.temporary);
        this.$parent.temporary = [];
      }
      this.$parent.getTableData(1, {});
      close(false);
    },
    onkeypress() {
      if (this.newFlag) {
        if (this.inputData.receiptkey != "") {
          this.$api
            .get(`asn/detailed/${this.inputData.receiptkey}`)
            .then((res) => {
              
                if (!res.bean) {
                  this.$message.error(this.$t('msg.msg55'));
                  this.inputData.receiptkey = "";
                } else {
                  this.inputData.storerkey = res.bean.storerkey;
                  this.$parent.flag = true;
                  this.$parent.getTableData(1, {});
                }
            });
        }
      }
    },
  },
  mounted() {
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.newFlag = !data.newFlag;
    } else {
      this.newFlag = !this.$store.state.newFlag;
    }
    //给input绑定回车事件
    this.$nextTick(() => {
      document.onkeydown = async (e) => {
        if (e.keyCode === 13) {
          this.onkeypress();
        }
      };
    });
  },
  watch: {
    $route: {
      handler(newValue, oldValue) {
        console.log(newValue, oldValue);
        // if (Object.keys(this.$store.state.skip).length !== 0) {
        //     let data = this.$store.state.skip;
        //     this.newFlag = !data.newFlag;
        // } else {
        //     this.newFlag = !this.$store.state.newFlag;
        // }
        // console.log(this.newFlag);
      },
      immediate: true,
      deep: true,
    },
  },
  beforeDestroy() {
    document.onkeydown = null;
  },
  components: {
    "v-details": details,
  },
};
</script>

<style scoped></style>
